# -*- coding: UTF-8 -*-

# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple python-docx
from docx import Document #导入库
# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pywin32
from win32com.client import Dispatch
import os

def demo(fullfile):
    document = Document(fullfile)  # 读入文件
    tables = document.tables  # 获取文件中的表格集
    table = tables[0]  # 获取文件中的第一个表格
    for row in range(0, len(table.rows)):  # 从表格第二行开始循环读取表格数据
        print("第%d行： \r\n" % row)
        for col in range(0, 7):
            result = "table.cell(%d, %d).text = %s \r\n" % (row, col, table.cell(row, col).text)
            print(result)

def read_CV_old(fullfile,coordinatelist):
    document = Document(fullfile)  # 读入文件
    tables = document.tables  # 获取文件中的表格集
    table = tables[0]  # 获取文件中的第一个表格
    snum = 1
    information = []
    information.append(fullfile)
    for site in coordinatelist:
        coordinate = site.split(",",1)
        point_x = int(coordinate[0])
        point_y = int(coordinate[1])
        name = table.cell(point_x,point_y).text
        information.append(name)
    return information


def read_CV_old1(fullfile):
    """

    :param fullfile: 文件所在路径
    :param cell_xy_list: 读取表格单元格坐标
    :return:
    """
    document = Document(fullfile)  # 读入文件
    tables = document.tables  # 获取文件中的表格集
    table = tables[0]  # 获取文件中的第一个表格
    # 1、基本信息
    print("1、基本信息：\r\n")
    # 姓名
    name = table.cell(1, 1).text
    # 性别
    sex = table.cell(1, 3).text
    # 年龄
    age = table.cell(1, 6).text
    # 任职时间
    hiredate = table.cell(2, 1).text
    # 岗位
    post = table.cell(2, 3).text
    # 部门
    dept = table.cell(2, 6).text
    # 最高学历
    highest_degree = table.cell(3, 1).text
    # 所学专业
    major = table.cell(3, 3).text
    # 从事行业
    industry = table.cell(3, 6).text
    print("姓名：%s，性别：%s，年龄：%s，任职时间：%s，岗位：%s，部门：%s，最高学历：%s，所学专业：%s，从事行业：%s" % (name, sex, age, hiredate, post, dept, highest_degree, major, industry))
    print("\r\n")
    # 2、教育背景
    print("2、教育背景：\r\n")
    education_list = []
    three_line = 7
    for edu_index in range(6,11):
        title = table.cell(edu_index, 0).text
        if title.__eq__("3、职称或证书"):
            three_line = edu_index
            break
        # 起止日期
        education_period = table.cell(6, 0).text
        # 学校名称
        education_school = table.cell(6, 2).text
        # 专业
        education_major = table.cell(6, 4).text
        # 获得学历/证书
        education_qualification = table.cell(6, 7).text
        education_info = [education_period,education_school,education_major,education_qualification]
        education_list.append(education_info)
    print(education_list)
    print("\r\n")
    #  3、职称或证书
    print("3、职称或证书：\r\n")
    title_professor_line = three_line + 1
    title_professor = table.cell(title_professor_line, 0).text
    print(title_professor)
    print("\r\n")
    # 4、专业技能
    print("4、专业技能：\r\n")
    professional_skill_line = title_professor_line + 2
    professional_skill = table.cell(professional_skill_line, 0).text
    print(professional_skill)
    print("\r\n")
    # 5、主要工作经验
    print("5、主要工作经验：\r\n")
    work_experience_line = professional_skill_line + 2
    work_experience = table.cell(work_experience_line, 0).text
    print(work_experience)
    print("\r\n")
    # 姓名，性别，年龄，任职时间，岗位，部门，最高学历，所学专业，从事行业，教育背景，职称或证书，专业技能，主要工作经验
    return name, sex, age, hiredate, post, dept, highest_degree, major, industry, education_list,title_professor, \
           professional_skill, work_experience


def write_CV_new(savefile):
    document = Document(savefile)  # 读入文件
    tables = document.tables  # 获取文件中的表格集
    table = tables[0]  # 获取文件中的第一个表格
    # 姓名
    table.cell(0, 1).text = "付信辉"
    # 职务
    table.cell(0, 3).text = "程序员"
    # 职称
    table.cell(0, 6).text = "高级Java工程师"
    # 年龄
    table.cell(1, 1).text = "29"
    # 拟任职
    table.cell(1, 3).text = "不知道"
    # 单位任职时间
    table.cell(1, 6).text = "2019-07-31"
    # 拟任职本项目岗位
    table.cell(2, 0).text = "拟任职本项目岗位：\r\n学历（毕业学校、时间、专业）及取得的专业认证情况："
    # 保存文档
    document.save(savefile)


def doc2docx(path):
    w = Dispatch('Word.Application')
    w.Visible = 0
    w.DisplayAlerts = 0
    doc = w.Documents.Open(path)
    newpath = os.path.splitext(path)[0] + '.docx'
    doc.SaveAs(newpath, 12, False, "", True, "", False, False, False, False)
    doc.Close()
    w.Quit()
    os.remove(path)
    return newpath


def updateActiveTimeAppover(wordpath, acitve_time, appover):

    fullfile = wordpath
    if not os.path.exists(wordpath):
        return
    extension = os.path.splitext(wordpath)[1]
    if extension.__eq__(".doc"):
        fullfile = doc2docx(wordpath)
    document = Document(fullfile)  # 读入文件
    tables = document.tables  # 获取文件中的表格集
    if len(tables) <= 0:
        return
    table = tables[0]  # 获取文件中的第一个表格
    last_version_line = 5
    for row_index in range(4,11):
        version = table.cell(row_index, 0).text
        print(version)
        if not version:
            last_version_line = row_index - 1
            break
        # 起止日期

    # 生效日期
    table.cell(last_version_line, 2).text = acitve_time
    # 批准人
    table.cell(last_version_line, 6).text = appover
    # 保存文档
    document.save(wordpath)
    if extension.__eq__(".doc"):
        os.remove(fullfile)


if __name__ == '__main__':
    # fullfile = "D:\\配置文件\\员工档案个人简历更换模板\\个人简历.docx"
    # 姓名，性别，年龄，任职时间，岗位，部门，最高学历，所学专业，从事行业，教育背景，职称或证书，专业技能，主要工作经验
    # read_CV_old(fullfile)
    #savefile = "D:\\配置文件\\员工档案个人简历更换模板\\个人简历 (新版).docx"
    #write_CV_new(savefile)
    docpath = "D:\\配置文件\\体系自动发文流程\\附件\\2020-07-15\\SP01-20200713-105\\YYKJ-JT-IT01-ZD01-MB01 XX信息系统应急预案模板V1.1.doc"
    acitve_time = "2020-07-16"
    appover = "付信辉"
    updateActiveTimeAppover(docpath, acitve_time, appover)